package top.yuechenc.manage.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.yuechenc.manage.common.token.ShopResult;
import top.yuechenc.manage.common.token.UserLoginToken;
import top.yuechenc.manage.entity.SpPermissionApi;
import top.yuechenc.manage.entity.SpPermissionDto;
import top.yuechenc.manage.service.PermissionApiService;
import top.yuechenc.manage.service.PermissionService;

import java.util.List;

/**
 * 权限管理接口
 *
 * @author Zhiwei Wang
 * @date 2020/2/17 20:01
 */
@Api(description = "权限管理接口")
@RestController
@RequestMapping(value = "/api")
public class PermissionController {
    private static final Logger log = LoggerFactory.getLogger(PermissionController.class);

    private static final String LIST = "list";

    private static final String TREE = "tree";

    @Autowired
    private PermissionService permissionService;
    @Autowired
    private PermissionApiService permissionApiService;

    @UserLoginToken
    @GetMapping("/rights")
    @ApiOperation(value = "获取权限信息")
    @ApiImplicitParam(name = "type", value = "返回类型", dataType = "string", paramType = "query", defaultValue = "list")
    private ShopResult rights(String type) {
        ShopResult result;
        try {
            if (LIST.equals(type)) {
                List<SpPermissionDto> permissionList = permissionService.findAll();
                for (SpPermissionDto spo : permissionList) {
                    SpPermissionApi spPermissionApi = permissionApiService.getByPsId(spo.getPsId());
                    spo.setPath(spPermissionApi.getPsApiPath());
                }
                result = new ShopResult(ShopResult.Metaa.SELECT, permissionList);
            } else if (TREE.equals(type)) {
                result = new ShopResult(ShopResult.Metaa.SELECT, findChildren("0"));
            } else {
                result = new ShopResult(ShopResult.Metaa.SELECT_FAIL, "请输入数据类型");
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.debug(e.toString());
            result = new ShopResult(ShopResult.Metaa.SELECT_FAIL, e);
            log.info("获取权限信息,result={}",result);
        }
        return result;
    }

    @UserLoginToken
    @ApiOperation(value = "获取左侧菜单")
    @GetMapping("/menus")
    private ShopResult menus() {
        ShopResult result=null;
        List<SpPermissionDto> permissionList = null;
        try {
            permissionList = permissionService.getByPid("0");
            for (SpPermissionDto spo : permissionList) {
                SpPermissionApi spPermissionApi = permissionApiService.getByPsId(spo.getPsId());
                spo.setPath(spPermissionApi.getPsApiPath());
                List<SpPermissionDto> child = permissionService.getByPid(spo.getPsId());
                for (SpPermissionDto sp : child) {
                    SpPermissionApi spa = permissionApiService.getByPsId(sp.getPsId());
                    sp.setPath(spa.getPsApiPath());
                }
                spo.setChildren(child);
            }
            result=new ShopResult(ShopResult.Metaa.SELECT, permissionList);
        } catch (Exception e) {
            e.printStackTrace();
            log.debug(e.toString());
            result=new ShopResult(ShopResult.Metaa.SELECT_FAIL, e);
        }
        return result;
    }

    private List<SpPermissionDto> findChildren(String pid) {
        List<SpPermissionDto> spoList = permissionService.getByPid(pid);
        if (spoList.size() > 0) {
            for (SpPermissionDto spol : spoList) {
                SpPermissionApi spPermissionApi = permissionApiService.getByPsId(spol.getPsId());
                spol.setPath(spPermissionApi.getPsApiPath());
                spol.setChildren(findChildren(spol.getPsId()));
            }
            return spoList;
        } else {
            return spoList;
        }

    }


}
